package com.yh.csx.crm.appapi.controller;

import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.exception.BusinessException;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.po.*;
import com.yh.csx.crm.qo.ClassRatioQo;
import com.yh.csx.crm.qo.ReportCreditWarningQo;
import com.yh.csx.crm.service.ReportFormsService;
import com.yh.csx.crm.service.SaleService;
import com.yh.csx.crm.service.request.*;
import com.yh.csx.crm.service.response.AccountReceivableResp;
import com.yh.csx.crm.service.response.CreditWarningResp;
import com.yh.csx.crm.service.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("报表查询服务")
@RestController
@RequestMapping("/reportForms")
public class ReportFormsController extends BaseController {
    @Autowired
    ReportFormsService reportFormsService;

    @Autowired
    SaleService saleService;

    @ApiOperation("获取业绩首页")
    @GetMapping("/index")
    public CommonResponse<ReportNewFormsResp> getRegion(ReportIndexReq req) {
        return super.visit(() -> {
            Long userId = super.getUserId();
            if (reportFormsService.getPower(userId)){
                //当权限为销售员时
                throw new BusinessException("你没有查看报表非业务员首页");
            }
            ReportHomeSale reportHomeSale = BeanUtils.convert(ReportHomeSale.class, req);
            reportHomeSale.setUserId(userId);
            return reportFormsService.getCustomer(reportHomeSale);
        });
    }

    @ApiOperation("获取业绩指标详情")
    @GetMapping("/resultTarget")
    public CommonResponse<ReportHomeSale> getResultTarget(ReportFormsReq req) {
        return super.visit(() -> {
            ReportHomeSale appHomeSale = BeanUtils.convert(ReportHomeSale.class, req);
            return reportFormsService.getResultInfo(appHomeSale);
        });
    }

    @ApiOperation("获取品占比详情")
    @GetMapping("/scale")
    public CommonResponse<List<ReportClassRatio>> getScale(ReportFormsReq req) {
        return super.visit(() -> {
            ClassRatioQo qo = BeanUtils.convert(ClassRatioQo.class, req);
            qo.setProvCodes(saleService.getPermitProvinceCodes(super.getUserId()));
            return reportFormsService.getClassSaleRationInfo(qo);
        });
    }

    @ApiOperation("获取业绩详情")
    @GetMapping("/result")
    public CommonResponse<List<ReportHomeSale>> getResult(ReportGetInfoReq req) {
        return super.visit(() -> {
            ReportHomeSale appHomeSale = BeanUtils.convert(ReportHomeSale.class, req);
            appHomeSale.setProvinceIds(saleService.getPermitProvinceCodes(super.getUserId()));
            return reportFormsService.getAppHomeSaleInfo(appHomeSale);
        });
    }

    @ApiOperation("省名获取城市业绩详情")
    @GetMapping("/result/city")
    public CommonResponse<List<ReportHomeSale>> getCityResult(ReportGetCitInfoReq req) {
        return super.visit(() -> {
            ReportHomeSale appHomeSale = BeanUtils.convert(ReportHomeSale.class, req);
            return reportFormsService.getCityResult(appHomeSale);
        });
    }


    @ApiOperation("获取大客户销售客户下单")
    @GetMapping("/bigSaleman")
    public CommonResponse<ReportBigSalemaResp> getBigSaleman(ShopCustlistsRep req) {
        return super.visit(() -> {
            ReportBigSaleman reportBigSaleman = BeanUtils.convert(ReportBigSaleman.class, req);
            reportBigSaleman.setSalesId(super.getWorkNumber());
            return reportFormsService.getBigSaleman(reportBigSaleman);
        });
    }

    @ApiOperation("获取餐饮销售")
    @GetMapping("/foodInfo")
    public CommonResponse<FoodInfoResp> getFoodInfo(ShopCustlistsRep req) {
        return super.visit(() -> {
            ReportFoodSale reportFoodSale = BeanUtils.convert(ReportFoodSale.class, req);
            reportFoodSale.setSalesId(super.getWorkNumber());
            return reportFormsService.getFoodInfo(reportFoodSale);
        });
    }

    @ApiOperation("获取M端销售")
    @GetMapping("/MInfo")
    public CommonResponse<ShopCustlistsResp> getMInfo(ShopCustlistsRep req) {
        return super.visit(() -> {
            ReportShopSale reportShopSale = BeanUtils.convert(ReportShopSale.class, req);
            reportShopSale.setSalesId(super.getWorkNumber());
            return reportFormsService.getMInfo(reportShopSale);
        });
    }

    @ApiOperation("获取所有渠道")
    @GetMapping("/getChannelsale")
    public CommonResponse<ReportGetChal> getChannelsale() {
        return super.visit(() -> reportFormsService.getChannelsale());
    }

    @ApiOperation("获取所有省")
    @GetMapping("/getProvinceName")
    public CommonResponse<ReportGetProNameResp> getProvinceName() {
        return super.visit(() -> reportFormsService.getProvinceName(super.getUserId()));
    }

    @ApiOperation("根据省名获取市")
    @GetMapping("/getCityName")
    public CommonResponse<ReportGetCityNameResp> getCityName(ReportGetCityNameReq req) {
        return super.visit(() -> reportFormsService.getCityName(req.getProvinceName()));
    }


    @ApiOperation("信控预警列表")
    @GetMapping("/warning/list")
    public CommonResponse<PageWrap<CreditWarningResp>> getWarningList(CreditWarningReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            ReportCreditWarningQo qo = BeanUtils.convert(ReportCreditWarningQo.class, req);
            return reportFormsService.getCreditWarningList(user, qo);
        });
    }

    @ApiOperation("应收账款列表")
    @GetMapping("/receivable/list")
    public CommonResponse<PageWrap<List<AccountReceivableResp>>> getReceivableList(ReprotAccountAgeReq req) {
        return super.visit(() -> {
            User user = new User(super.getUserId(), super.getUserName());
            return reportFormsService.getAccountAgeList(user, req);
        });

    }
}
